<template>
  <div class="be-task">
    <el-select v-model="selectTaskTypeValue" filterable placeholder="请选择问题类型(可搜索)"
               class="select-task-type" @change="selectTaskType">
      <el-option
        v-for="item in selectTaskTypeList"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <el-button type="primary" icon="el-icon-delete" @click="cleanSelectTaskType"></el-button>

    <el-table :data="taskList" border stripe class="item-table">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="center" prop="userType" label="用户组" width="65"></el-table-column>
      <el-table-column align="center" prop="userId" label="用户ID" width="100">
        <template scope="scope">
          <p style="color: #409EFF;">{{scope.row.userId}}</p>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="userInfo.realName" label="用户姓名" width="80"></el-table-column>
      <el-table-column align="center" prop="userInfo.cell" label="用户电话" width="115"></el-table-column>
      <el-table-column align="center" prop="taskName" label="问题类型"></el-table-column>
      <el-table-column align="center" prop="dayId" label="问题时间" width="90"></el-table-column>
      <el-table-column align="center" prop="taskMessage" label="问题信息"></el-table-column>

      <el-table-column align="center" prop="operateStatus" label="处理状态" width="120">
        <template scope="scope">
          <el-tag v-if="scope.row.operateStatus === 1" size="mini" type="success">未处理</el-tag>
          <el-tag v-else-if="scope.row.operateStatus === 2" size="mini">跟进中</el-tag>
          <el-tag v-else-if="scope.row.operateStatus === 3" size="mini" type="info">已完成</el-tag>
          <el-tag v-else-if="scope.row.operateStatus === 4" size="mini" type="info">无需处理</el-tag>
          <el-tag v-else-if="scope.row.operateStatus === 5" size="mini" type="info">已加微信</el-tag>
          <el-tag v-else-if="scope.row.operateStatus === 7" size="mini" type="danger">处理失败</el-tag>
          <el-tag v-else size="mini" type="danger">未知</el-tag>

          <el-select v-model="operateStatusValue" placeholder="更新" size="mini"
                     style="margin-top: 10px" @change="updateOperateStatusValue(scope.row)">
            <el-option
              v-for="item in operateStatusOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>

        </template>
      </el-table-column>

      <el-table-column align="center" prop="operateor" label="处理人"></el-table-column>
      <el-table-column align="center" prop="operateTime" label="处理时间"></el-table-column>
      <el-table-column align="center" prop="operateMessage" label="处理说明"></el-table-column>

      <el-table-column align="center" prop="status" label="状态" width="80px">
        <template scope="scope">
          <el-tag size="mini">已分配</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="page.current"
        :page-size="page.size"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  export default {
    components: {},
    name: 'BeTask',
    data () {
      return {
        // 问题类型搜索
        selectTaskTypeList: [],
        selectTaskTypeValue: '',

        taskList: [],
        page: {
          current: 1,
          size: 10,
          total: 0
        },
        pageIndex: 0, // 0 任务列表， 1 选择问题类型任务列表
        operateStatusOptions: [
          { value: 1, label: '未处理' },
          { value: 2, label: '跟进中' },
          { value: 3, label: '已完成' },
          { value: 4, label: '无需处理' },
          { value: 5, label: '已加微信' },
          { value: 7, label: '处理失败' }
        ],
        operateStatusValue: ''
      }
    },
    methods: {
      /** 页数修改处理 */
      handleSizeChange(val) {
        this.page.current = 1;
        this.page.size = val;
        this.loadTaskList();
      },
      handleCurrentChange() {
        this.loadTaskList();
      },

      loadTaskList() {
        let url = '';
        let param = {};
        switch (this.pageIndex) {
          case 1: {
            url = "/api/task/page/list/taskcode/2";
            param = { taskCode: this.selectTaskTypeValue, current: this.page.current, size: this.page.size };
            break;
          }
          case 0:
          default: {
            url = "/api/task/page/list/2";
            param = { current: this.page.current, size: this.page.size };
            break;
          }
        }

        this.$http.post(url, param, {emulateJSON: true}).then((res) => {
          if (!this.checkResponse(res)) {
            return;
          }

          let page = res.body.data;
          this.page.total = page.total;
          this.taskList = page.data;
        }).catch((e) => { this.messageException(e); });

      },
      test(data) {
        this.page.total = data.total;
        this.taskList = data.data;
      },

      loadSelectTaskTypeList() {
        this.$http.get("/api/type/list", {}, {emulateJSON: true}).then((res) => {
          if (!this.checkResponse(res)) {
            return;
          }

          this.selectTaskTypeList = [];
          let typeList = res.body.data;
          for (let i = 0; i < typeList.length; i++) {
            this.selectTaskTypeList.push({
              value: typeList[i].taskCode,
              label: '(' + (i+1) + ') ' + typeList[i].taskName
            });
          }
        }).catch((e) => { this.messageException(e); });
      },

      // 问题类型选择
      selectTaskType() {
        this.pageIndex = 1;
        this.page.current = 1;
        this.loadTaskList();
      },
      cleanSelectTaskType() {
        this.pageIndex = 0;
        this.selectTaskTypeValue = '';
        this.page.current = 1;
        this.loadTaskList();
      },
      // 更新单个任务操作状态
      updateOperateStatusValue(task) {
        if (this.operateStatusValue === 0 || this.operateStatusValue === task.operateStatus) {
          return;
        }

        this.$http.put("/api/task", { id:task.id, operateStatus:this.operateStatusValue }, {emulateJSON: true}).then((res) => {
          if (!this.checkResponse(res)) {
            return;
          }

          this.loadTaskList();
        }).catch((e) => { this.messageException(e); });
        this.operateStatusValue = '';
      }
    },
    created() {
      this.pageIndex = 0;
      this.loadTaskList();
      this.loadSelectTaskTypeList();
    }
  }
</script>

<style lang="less" scoped>
  .be-task {
  }

  .select-task-type {
    margin-top: 10px;
    margin-left: 1%;
    margin-bottom: 10px;
    color: red;
  }
</style>
